package com.carbohydrate42.kotlin_compose_demo.compose

import android.content.Context
import android.media.MediaPlayer
import android.os.Build
import android.os.Vibrator
import android.os.VibratorManager
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.carbohydrate42.kotlin_compose_demo.R

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun TryVibrateAndSound() {
    val context = LocalContext.current

    val vibrateDuration = 100L

    val viber = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
        vibratorManager.defaultVibrator;
    } else {
        context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    }

    val mediaPlayer1: MediaPlayer? = MediaPlayer.create(context, R.raw.beep)
    val mediaPlayer2: MediaPlayer? = MediaPlayer.create(context, R.raw.success)
    val mediaPlayer3: MediaPlayer? = MediaPlayer.create(context, R.raw.fail)

    Column() {
        Button(onClick = { viber.vibrate(vibrateDuration) }) {
            Text(text = "vibrate")
        }

        Button(onClick = { mediaPlayer1?.start() }) {
            Text(text = "beep")
        }

        Button(onClick = { mediaPlayer2?.start() }) {
            Text(text = "success")
        }

        Button(onClick = { mediaPlayer3?.start() }) {
            Text(text = "fail")
        }

    }



}